package pl.edu.agh.persdoc;

import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import pl.edu.agh.persdoc.model.Examination;
import pl.edu.agh.persdoc.model.ExaminationRepository;
import pl.edu.agh.persdoc.model.RegistrationKey;
import pl.edu.agh.persdoc.model.RegistrationKeyRepository;
import pl.edu.agh.persdoc.model.Schedule;
import pl.edu.agh.persdoc.model.ScheduleRepository;
import pl.edu.agh.persdoc.schedule.Scenario;
import pl.edu.agh.persdoc.schedule.ScheduleDeserializer;


@Controller
public class PersonalDocController {

	@Autowired
	ExaminationRepository examinationRepo;
	
	@Autowired
	ScheduleRepository scheduleRepo;

	@Autowired
	RegistrationKeyRepository registrationKeyRepo;
	
	@Autowired
	ExaminationsProcessor examinationProcessor;
	
	@Autowired
	ScheduleDeserializer scheduleDeserializer;
	
	
	
	//TODO change 'exams' to empty or personal-doc,clean up the mess with uris
	@RequestMapping( value = "/exams/", method = RequestMethod.GET)
	public String viewAll(Model m) {
		m.addAttribute("patients",examinationRepo.getAllPatients());
		return "/exams/index";
	}
	
	@RequestMapping( value = "/exams/{patient}", method = RequestMethod.GET)
	public String showResultsForPatient(@PathVariable("patient") String patient,Model m) {

		m.addAttribute("patientName", patient);
		Map<String,String> namesAndGraphUrls= examinationProcessor.createGraphsForPatient(examinationRepo.findByPatientID(patient));
		m.addAttribute("results",namesAndGraphUrls);
		return "/exams/results";
	}
	
	
	@RequestMapping( value = "/schedules", method = RequestMethod.GET)
	public String showSchedule(Model m) {
		
		String currentScheduleAsJSON=scheduleRepo.getLatestSchedule();
		System.out.println("current schedule json:"+currentScheduleAsJSON);
		List<Scenario> scenariosList=null;
		if(currentScheduleAsJSON!=null){
			scenariosList=scheduleDeserializer.deserializeToSchedule(currentScheduleAsJSON);
			System.out.println("number of scenarios:"+scenariosList.size());
			m.addAttribute("scenarios",scenariosList);
		}
		m.addAttribute("scenarios",scenariosList);
		return "/schedules/index";
		
	}
	
	@RequestMapping( value = "/schedules/new/{newSchedule}", method = RequestMethod.POST)
	public @ResponseBody String storeSchedule(@PathVariable("newSchedule") String newSchedule) {

		Schedule newScheduleRecord=new Schedule();	
		newScheduleRecord.setSchedule(newSchedule);
		newScheduleRecord.setCreationDate(new DateTime());
		scheduleRepo.add(newScheduleRecord);
		return "schedule stored";
		
	}
	
	//-------------------Communcation with cell phones--------------------------
	/**
	 * Method used to send current registration key of mobile phone to the server. 
	 * This key will be stored in database as it is necessary for c2dm communication
	 * @deprecated
	 */
	@RequestMapping( value = "/registration/{key}")
	public String storeRegistrationID(@PathVariable("key") String key) {
		
		System.out.println("new key:"+key);
		RegistrationKey registrationKey=new RegistrationKey();
		registrationKey.setRegistrationKey(key);
		registrationKey.setCreationDate(new DateTime());
		registrationKeyRepo.add(registrationKey);
		return "/exams/keyUpdated";
		
	}

	/**
	 * Returns current schedule as a string. This string is a json object and is put inside response body
	 */
	@RequestMapping( value = "/schedules/current", method = RequestMethod.GET)
	public @ResponseBody String getCurrentSchedule() {
		
		System.out.println("invoked!");
		return 	scheduleRepo.getLatestSchedule();
		
	}
	/**
	 * Saves exmaination results sent by mobile devices.
	 */
	@RequestMapping( value = "/results/new", method = RequestMethod.POST)
	public @ResponseBody String sendExaminationResults(
			@RequestParam("patientID") String patientID,
			@RequestParam("name") String name,
			@RequestParam("value") String value,
			@RequestParam("timestamp") String timestamp,
			@RequestParam("measurementID") String measurementID)
			 
   {
		
		Examination examination=new Examination();
		examination.setPatientID(patientID);
		examination.setName(name);
		examination.setTimestamp(Util.convertStringToDate(timestamp));
		examination.setValue(Integer.parseInt(value));
		examinationRepo.add(examination);
		return "new result stored!";
		
	}

}
